package com.bishe.app.modules.groupuser.controller;

import com.bishe.app.core.api.CommonResult;
import com.bishe.app.core.api.CommonPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bishe.app.core.enums.IsDel;
import com.bishe.app.core.utils.Lock;
import com.bishe.app.modules.group.service.GroupService;
import com.bishe.app.modules.groupuser.model.GroupUserBatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import com.bishe.app.modules.groupuser.service.GroupUserService;
import com.bishe.app.modules.groupuser.entity.GroupUser;

import java.util.List;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * <p>
 * 组织-用户表 前端控制器
 * </p>
 *
 * @author
 * @since 2024-03-13
 */
@RestController
@RequestMapping("/groupUser")
public class GroupUserController {

    @Autowired
    public GroupUserService groupUserService;

    @Resource
    private GroupService groupService;

    @RequestMapping(value = "/updateUserLeader", method = RequestMethod.POST)
    @ResponseBody
    @Lock(lockPrefix = "/groupUser",key = "lock", lockSecond = 60)
    public CommonResult updateUserLeader(@RequestBody GroupUser group) {
        groupUserService.lambdaUpdate()
                .set(GroupUser::getLeader, group.getLeader())
                .eq(GroupUser::getGroupId, group.getGroupId())
                .eq(GroupUser::getUserId, group.getUserId())
                .update();
        return CommonResult.success(null);
    }


    @RequestMapping(value = "/delGroupUser", method = RequestMethod.POST)
    @ResponseBody
    @Lock(lockPrefix = "/groupUser",key = "lock", lockSecond = 60)
    public CommonResult delGroupUser(@RequestBody GroupUser group) {
        groupUserService.lambdaUpdate()
                .set(GroupUser::getIsDel, IsDel.Y.getCode())
                .eq(GroupUser::getGroupId, group.getGroupId())
                .eq(GroupUser::getUserId, group.getUserId())
                .update();
        return CommonResult.success(null);
    }


    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    @Lock(lockPrefix = "/groupUser",key = "lock", lockSecond = 60)
    public CommonResult create(@RequestBody GroupUser groupUser) {
        boolean success = groupUserService.save(groupUser);
        if (success) {
            return CommonResult.success(null);
        }
        return CommonResult.failed();
    }

    @RequestMapping(value = "/batchCreate", method = RequestMethod.POST)
    @ResponseBody
    @Lock(lockPrefix = "/groupUser",key = "lock", lockSecond = 60)
    public CommonResult create(@RequestBody GroupUserBatch groupUser) {
        // 如果用户已经有了，就不再添加

        for (Long userId : groupUser.getUserIds()) {
            List<GroupUser> groupUserList = groupUserService.lambdaQuery()
                    .eq(GroupUser::getUserId, userId)
//                    .eq(GroupUser::getGroupId, groupUser.getGroupId())
                    .eq(GroupUser::getIsDel, IsDel.N.getCode())
                    .list();


            if (CollectionUtils.isEmpty(groupUserList)) {
                GroupUser gu = new GroupUser();
                gu.setUserId(userId);
                gu.setGroupId(groupUser.getGroupId());
                gu.setLeader(groupUser.getLeaderId());

                groupUserService.save(gu);
            } else {
                for (GroupUser user : groupUserList) {
                    groupUserService.lambdaUpdate()
                            .eq(GroupUser::getGroupId, user.getGroupId())
                            .eq(GroupUser::getUserId, user.getUserId())
                            .set(GroupUser::getIsDel, IsDel.E.getCode())
                            .update();
                }
                GroupUser gu = new GroupUser();
                gu.setUserId(userId);
                gu.setGroupId(groupUser.getGroupId());
                gu.setLeader(groupUser.getLeaderId());

                groupUserService.save(gu);
            }
        }
        return CommonResult.success(null);
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    @Lock(lockPrefix = "/groupUser",key = "lock", lockSecond = 60)
    public CommonResult update(@PathVariable Long id, @RequestBody GroupUser groupUser) {
        groupUser.setId(id);
        boolean success = groupUserService.updateById(groupUser);
        if (success) {
            return CommonResult.success(null);
        }
        return CommonResult.failed();
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    @Lock(lockPrefix = "/groupUser",key = "lock", lockSecond = 60)
    public CommonResult delete(@PathVariable Long id) {
        boolean success = groupUserService.removeById(id);
        if (success) {
            return CommonResult.success(null);
        }
        return CommonResult.failed();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult deleteBatch(@RequestParam("ids") List<Long> ids) {
        boolean success = groupUserService.removeByIds(ids);
        if (success) {
            return CommonResult.success(null);
        }
        return CommonResult.failed();
    }


    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<GroupUser>> listAll() {
        List<GroupUser> groupUserList = groupUserService.list();
        return CommonResult.success(groupUserList);
    }

    @RequestMapping(value = "/pageList", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<GroupUser>> page(@RequestBody CommonPage commonPage) {
        Page<GroupUser> page = new Page(commonPage.getPageNum(), commonPage.getPageSize());
        return CommonResult.success(CommonPage.restPage(groupUserService.page(page)));
    }
}



